import axios from 'axios'
import qs from 'qs'
import store from '../store/index.js'

const JUHE_APPKEY = '0d3e1325d5165fc31964c5f24938366c'

const axiosPost = options => {
	axios.post(
		options.url,
		qs.stringify({
			key: JUHE_APPKEY,
			...options.data
		})
	).then(res => {
		// console.log('2222')
		options.success(res)
		 // console.log(res)
	}).catch(err => {
		options.error(err)
	})
}

const axiosGet = options => {
	axios.get(
		options.url,
		{
			params:{
				key: JUHE_APPKEY,
				...options.data
			}
		}
	).then(res => {
		options.success(res)
	}).catch(err => {
		options.error(err)
	})
}

const getNew = type => {
	return new Promise((resolve,reject) => {
		axiosPost({
			url: '/api/toutiao/index',
			data: {
				type
			},
			success:function(data){
				resolve(data)
				 // console.log(data)   这里可以拿到数据
				// store.state.shehuiNewList = data.data.result.data
				// console.log(store.state.shehuiNewList)
				 // newL = data.data.result.data
				 // console.log(newL)
			},
			error:function(err){
				reject(err)
			}
		})
	// console.log(data)  data找不到
	})
}

const nowNew = async (type) =>{
	var data = null
	switch(type){
		case 'shehui':
			data = await getNew(type)
			store.state.shehuiNewList = data.data.result.data
			break
		case 'guonei':
			data = await getNew(type)
			store.state.guoneiNewList = data.data.result.data
			// console.log(store.state.guoneiNewList)
			break
		case 'yule':
			data = await getNew(type)
			store.state.yuleNewList = data.data.result.data
			// console.log(store.state.yuleNewList)
			break
		case 'keji':
			data = await getNew(type)
			store.state.kejiNewList = data.data.result.data
			// console.log(store.state.kejiNewList)
			break
		case 'junshi':
			data = await getNew(type)
			store.state.junshiNewList = data.data.result.data
			// console.log(store.state.junshiNewList)
			break
		case 'caijin':
			data = await getNew(type)
			store.state.caijinNewList = data.data.result.data
			// console.log(store.state.caijinNewList)
			break
		case 'shishang':
			data = await getNew(type)
			store.state.shishangNewList = data.data.result.data
			// console.log(111)
			// console.log(store.state.shishangNewList)
			break
	}
}

export{
	axiosPost,
	axiosGet,
	getNew,
	nowNew
}